package com.qk.controller;

import com.qk.aop.anno.LogOperation;
import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.dto.UserDTO;
import com.qk.entity.User;
import com.qk.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 28188
 */
@Slf4j
@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping
    public Result pageUsers(UserDTO userDTO) {
        PageResult<User> userPage = userService.pageUsers(userDTO);
        return Result.success(userPage);
    }

    @LogOperation
    @PostMapping
    public Result addUser(@RequestBody User user) {
        log.info("新增用户: {}", user);
        userService.saveUser(user);
        return Result.success();
    }

    @LogOperation
    @DeleteMapping("/{ids}")
    public Result deleteUser(@PathVariable List<Integer> ids) {
        log.info("删除用户: {}", ids);
        userService.removeUser(ids);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getUserById(@PathVariable Integer id) {
        log.info("根据ID查询用户: {}", id);
        User user = userService.getUserById(id);
        return Result.success(user);
    }

    @LogOperation
    @PutMapping
    public Result updateUser(@RequestBody User user) {
        log.info("修改用户: {}", user);
        userService.updateUser(user);
        return Result.success();
    }

    @GetMapping("/list")
    public Result listAllUsers() {
        log.info("查询所有用户");
        List<User> users = userService.listAllUsers();
        return Result.success(users);
    }

    @GetMapping("/role/{roleLabel}")
    public Result listUsersByRole(@PathVariable String roleLabel) {
        log.info("根据角色查询用户: {}", roleLabel);
        List<User> users = userService.listUsersByRole(roleLabel);
        return Result.success(users);
    }

    @GetMapping("/dept/{deptId}")
    public Result listUsersByDept(@PathVariable Integer deptId) {
        log.info("根据部门查询用户: {}", deptId);
        List<User> users = userService.listUsersByDept(deptId);
        return Result.success(users);
    }
}